#include "jsonwindow.h"
#include "ui_jsonwindow.h"
#include <QDebug>
JsonWindow::JsonWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::JsonWindow)
{
    ui->setupUi(this);
//    parseJson();
    zuzhiJson();
}

JsonWindow::~JsonWindow()
{
    delete ui;
}

void JsonWindow::parseJson()
{
    QFile file("../empty/json.txt");
    file.open(QIODevice::ReadOnly);

    QByteArray res = file.readAll();

    QJsonDocument doc = QJsonDocument::fromJson(res);

    QJsonObject root = doc.object();

    QString code = root.value("transCode").toString();
    QString name = root.value("name").toString();

    QJsonArray scores = root.value("score").toArray();
    QVector<int> sc;
    for(int i=0;i<scores.size();i++)
    {
        sc.push_back(scores.at(i).toInt());
    }
    qDebug()<<"score:"<<sc;
    QJsonObject addr = root.value("addr").toObject();

    QString sheng = addr.value("sheng").toString();
    QString shi = addr.value("shi").toString();
    QString qu = addr.value("qu").toString();

    qDebug()<<code<<","<<name<<","<<sheng<<","<<shi<<","<<qu;
}

void JsonWindow::zuzhiJson()
{
    QJsonObject root;
    root.insert("transCode",QJsonValue("001"));
    root.insert("name","lucy");
    QJsonArray arr;
    arr.append(10);
    arr.append(20);
    arr.append(30);
    root.insert("score",QJsonValue(arr));

    QJsonObject addrObj;
    addrObj.insert("sheng","shanxi");
    addrObj.insert("shi","xian");
    addrObj.insert("qu","beilin");
    root.insert("addr",QJsonValue(addrObj));

    QJsonDocument doc(root);

    qDebug()<< doc.toJson();
    qDebug()<<doc;
}
